package trong.gold.soft.com.view;

import trong.gold.soft.com.model.Resources;
import trong.gold.soft.com.model.drawBackgr;
import trong.gold.soft.com.model.mediaGame;
import trong.gold.soft.com.main.mTetrisMidlet;
import trong.gold.soft.com.model.drawBackgr;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;

import trong.gold.soft.com.model.createImg;
import trong.gold.soft.com.model.font.VietnameseFont;

public class slSound extends GameCanvas{
	private Graphics g;
	private Image backgr;
	private String backgrName = "splash1.png";
	private static boolean sl=true;
	private String req="Bạn có muốn bật âm thanh ?",noks="Không";
	private static mediaGame media;
	private int latx=0,laty=0;
	private mTetrisMidlet midlet;
	private drawBackgr back;
	protected slSound(mTetrisMidlet midlet) {
		super(false);
		setFullScreenMode(true);
		this.midlet = midlet;
		back = new drawBackgr();
		this.g = this.getGraphics();
		System.out.println(this.getWidth());
		System.out.println(this.getHeight());
		//backgr = new ResizeImage().resize(createImg.createImage("/Image/background.jpg"), getWidth(), getHeight());
		backgr = createImg.createImage("/com/image/load/" + backgrName);
		media = new mediaGame();
		
		Draw();
	}
	private void Draw(){
		//g.drawImage(backgr, 0, 0, Resources.TOP_LEFT_ANCHOR);
		g.setColor(Resources.black);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		back.drawbackgr(this.getWidth(), this.getHeight(), 13, backgr, g, 27, 34);
		g.setColor(0xb8f1e2);
		g.fillRect(this.getWidth()/2 - (VietnameseFont.Black.getWidth(req) + 20)/2, this.getHeight()/2 - VietnameseFont.Black.getHeight()/2, VietnameseFont.Black.getWidth(req) + 20, 4*VietnameseFont.Black.getHeight());
		VietnameseFont.WhiteBold.DrawString(g, "Chọn",this.getWidth()/2, this.getHeight() - VietnameseFont.WhiteBold.getHeight(), Graphics.HCENTER);
		VietnameseFont.Black.DrawString(g, req, this.getWidth()/2 - (VietnameseFont.Black.getWidth(req) + 20)/2 + VietnameseFont.White.getWidth(req)/2, this.getHeight()/2, Graphics.HCENTER);
		g.setColor(0xf51e1e);
		g.drawRect(this.getWidth()/2 - VietnameseFont.Black.getWidth(noks), this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(), VietnameseFont.Black.getWidth(noks), VietnameseFont.Black.getHeight());
		g.drawRect(this.getWidth()/2 + VietnameseFont.Black.getWidth(noks)/2, this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(), VietnameseFont.Black.getWidth(noks), VietnameseFont.Black.getHeight());
		g.fillRect(this.getWidth()/2 - VietnameseFont.Black.getWidth(noks),this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(),VietnameseFont.Black.getWidth(noks), VietnameseFont.Black.getHeight());
		VietnameseFont.BlackBold.DrawString(g, "Có",this.getWidth()/2 - VietnameseFont.Black.getWidth(noks)/2,this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(), Graphics.HCENTER);
		VietnameseFont.BlackBold.DrawString(g, "Không",this.getWidth()/2 + VietnameseFont.Black.getWidth(noks),this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(), Graphics.HCENTER);
		flushGraphics();
	}
	private void clear(){
		g.setColor(0xb8f1e2);
		g.fillRect(this.getWidth()/2 - VietnameseFont.Black.getWidth(noks),this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(),VietnameseFont.Black.getWidth(noks), VietnameseFont.Black.getHeight());
		g.fillRect(this.getWidth()/2 + VietnameseFont.Black.getWidth(noks)/2,this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(),VietnameseFont.Black.getWidth(noks), VietnameseFont.Black.getHeight());
		VietnameseFont.BlackBold.DrawString(g, "Có",this.getWidth()/2 - VietnameseFont.Black.getWidth(noks)/2,this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(), Graphics.HCENTER);
		VietnameseFont.BlackBold.DrawString(g, "Không",this.getWidth()/2 + VietnameseFont.Black.getWidth(noks),this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(), Graphics.HCENTER);
		flushGraphics();
	}
	private void redraw(){
		clear();
		if(sl){
			g.setColor(0xf51e1e);
			g.fillRect(this.getWidth()/2 + VietnameseFont.Black.getWidth(noks)/2,this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(),VietnameseFont.Black.getWidth(noks), VietnameseFont.Black.getHeight());
			VietnameseFont.BlackBold.DrawString(g, "Không",this.getWidth()/2 + VietnameseFont.Black.getWidth(noks),this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(), Graphics.HCENTER);
			sl=false;
		}else{
			g.setColor(0xf51e1e);
			g.fillRect(this.getWidth()/2 - VietnameseFont.Black.getWidth(noks),this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(),VietnameseFont.Black.getWidth(noks), VietnameseFont.Black.getHeight());
			VietnameseFont.BlackBold.DrawString(g, "Có",this.getWidth()/2 - VietnameseFont.Black.getWidth(noks)/2,this.getHeight()/2 + 2*VietnameseFont.Black.getHeight(), Graphics.HCENTER);
			sl=true;
		}
		flushGraphics();
	}
	public void keyPressed(int keyCode){
		switch (getGameAction(keyCode)) {
			case LEFT:
				if(!sl)
					redraw();
				break;
			case RIGHT:
				if(sl)
					redraw();
				break;
			case FIRE:
				
				goimenu();
				break;
		}
	}
	private void goimenu() {
		if(sl)
			media.startMedia();
		else
			media.stopMedia();
		setnull();
		Runtime.getRuntime().gc();
		midlet.switchState(this,new menuTetris(midlet));
	}
	public void pointerPressed(int x,int y){
		latx=x;
		laty=y;
			for(int i=this.getWidth()/2 - VietnameseFont.Black.getWidth(noks);i<=this.getWidth()/2 - VietnameseFont.Black.getWidth(noks) + VietnameseFont.Black.getWidth("noks");i++)	
				for(int j=this.getHeight()/2 + 2*VietnameseFont.Black.getHeight();j<=this.getHeight()/2 + 2*VietnameseFont.Black.getHeight() + VietnameseFont.Black.getHeight();j++){
					if(i==latx && j==laty){
						sl=false;
						redraw();

						
					}
				}
				for(int i=this.getWidth()/2 + VietnameseFont.Black.getWidth(noks)/2;i<=this.getWidth()/2 + VietnameseFont.Black.getWidth(noks)/2 + VietnameseFont.Black.getWidth(noks);i++)
					for(int j=this.getHeight()/2 + 2*VietnameseFont.Black.getHeight();j<=this.getHeight()/2 + 2*VietnameseFont.Black.getHeight() + VietnameseFont.Black.getHeight();j++){
						if(i==latx && j==laty){
							sl=true;
							redraw();
						}
				}
			
			goimenu();
	}
	public static boolean getsound(){
		return sl;
	}
	public static boolean setsound(){
		if(sl)
			sl=false;
		else
			sl=true;
		return sl;
	}
	public static mediaGame getmedia(){
		return media;
	}
	private void setnull(){
		g = null;
		backgr=null;
		backgrName=null;
		req=null;
		noks=null;
		back=null;
	}
}